//文字 继承自 zrender.Displayable
zrender = window.zrender;
let Text
const DrawUtil = {
  // 初始化悬浮文本
  initHoverText: function (zr) {
    // eslint-disable-next-line prefer-const
    Text = new zrender.Text({
      style: {
        fontSize: 14,
        textBackgroundColor: '#FFF',
        textBorderColor: '#000',
        textBorderWidth: 1,
        textAlign: 'center',
        textBorderRadius: 2,
        textPadding: 5
      },
      zlevel: 100
    })
    zr.add(Text)
    Text.hide()
  },
  /**
   * 绘制文本
   * @param Obj       基础参数  {x:横下坐标,y:纵向坐标,text:'文本内容',fontSize:字体大小}
   * @param direction 文字方向 （T=横向；L=纵向）
   * @param Color     颜色
   * @returns {*|module:zrender/graphic/Group|Group}
   */
  drawText: function (Obj, direction, Color) {
    let text
    let y
    const group = new zrender.Group()
    if (direction === 'L') { //纵向文本处理
      for (let i = 0; i < Obj.text.length; i++) {
        text = new zrender.Text({
          style: {
            text: Obj.text.charAt(i),
            fontSize: Obj.fontSize,
            textFill: Color,
            textAlign: Obj.textAlign,
            fontWeight: Obj.fontWeight,
            fontFamily: Obj.fontFamily,
            textPosition: Obj.textPosition
          }
        }).on('mouseover', function () {
          Text.show()

          Text.attr('position', [Obj.x + (Obj.text.length * 14 / 2), Obj.y - 25])
          Text.attr({
            style: {
              text: Obj.hint
            }
          })
        }).on('mouseout', function () {
          Text.hide()
        })
        if (Obj.lineHeight) {
          y = Obj.lineHeight * i
        } else {
          y = 16 * i
        }
        text.attr('position', [0, y])
        group.add(text)
      }
    } else { //横向文本处理
      text = new zrender.Text({
        style: {
          text: Obj.text,
          fontSize: Obj.fontSize,
          textFill: Color,
          textAlign: Obj.textAlign,
          fontWeight: Obj.fontWeight,
          fontFamily: Obj.fontFamily,
          truncate: Obj.truncate
        }
      }).on('mouseover', function () {
        Text.show()
        if (Obj.text && Obj.text !== null) {
          Text.attr('position', [Obj.x + (Obj.text.length * 14 / 2), Obj.y - 25])
          Text.attr({
            style: {
              text: Obj.hint
            }
          })
        }
      }).on('mouseout', function () {
        Text.hide()
      })
      group.add(text)
    }
    group.attr('position', [Obj.x, Obj.y])
    return group
  },
  // 绘制实心圆 ⬤
  drawCircle: function (Obj, Color) {
    return new zrender.Circle({
      shape: {
        cx: Obj.x,
        cy: Obj.y,
        r: 4
      },
      style: {
        fill: Color
      }
    }).on('mouseover', function () {
      Text.show()

      Text.attr('position', [Obj.x, Obj.y - 32])
      Text.attr({
        style: {
          text: Obj.txt
        }
      })
    }).on('mouseout', function () {
      Text.hide()
    })
  },
  // 绘制实心三角形 ▲
  drawIsogon: function (Obj, Color) {
    return new zrender.Isogon({
      shape: {
        x: Obj.x,
        y: Obj.y,
        r: 6,
        n: 3
      },
      style: {
        fill: Color
      }
    }).on('mouseover', function () {
      Text.show()

      Text.attr('position', [Obj.x, Obj.y - 32])
      Text.attr({
        style: {
          text: Obj.txt
        }
      })
    }).on('mouseout', function () {
      Text.hide()
    })
  },
  // 绘制实心三角形 △
  drawHollowIsogon: function (Obj, Color) {
    return new zrender.Isogon({
      shape: {
        x: Obj.x,
        y: Obj.y,
        r: 6,
        n: 3
      },
      style: {
        fill: '#FFFFFF',
        stroke: Color
      }
    }).on('mouseover', function () {
      Text.show()

      Text.attr('position', [Obj.x, Obj.y - 32])
      Text.attr({
        style: {
          text: Obj.txt
        }
      })
    }).on('mouseout', function () {
      Text.hide()
    })
  },
  // 绘制空心圆 ⭘
  drawHollowCircle: function (Obj, Color) {
    return new zrender.Circle({
      shape: {
        cx: Obj.x,
        cy: Obj.y,
        r: 4
      },
      style: {
        fill: '#FFFFFF',
        stroke: Color
      }
    }).on('mouseover', function () {
      Text.show()

      Text.attr('position', [Obj.x, Obj.y - 32])
      Text.attr({
        style: {
          text: Obj.txt
        }
      })
    }).on('mouseout', function () {
      Text.hide()
    })
  },
  // 绘制 X
  drawX: function (Obj, Color) {
    return new zrender.Text({
      style: {
        text: '×',
        fontSize: 14,
        textFill: Color
      },
      position: [Obj.x - 5, Obj.y - 7]
    }).on('mouseover', function () {
      Text.show()

      Text.attr('position', [Obj.x, Obj.y - 32])
      Text.attr({
        style: {
          text: Obj.txt
        }
      })
    }).on('mouseout', function () {
      Text.hide()
    })
  },
  // 绘制 V
  drawV: function (Obj, Color) {
    return new zrender.Text({
      style: {
        text: 'v',
        fontSize: 16,
        fontFamily: 'Georgia',
        textFill: Color
      },
      position: [Obj.x - 5, Obj.y - 19]
    }).on('mouseover', function () {
      Text.show()

      Text.attr('position', [Obj.x, Obj.y - 32])
      Text.attr({
        style: {
          text: Obj.txt
        }
      })
    }).on('mouseout', function () {
      Text.hide()
    })
  },
  // 绘制圆圈中有点的圆 ⊙
  drawPointCircle: function (Obj, Color) {
    var group = new zrender.Group()
    var Point = new zrender.Circle({
      shape: {
        cx: 4,
        cy: 4,
        r: 1
      },
      style: {
        fill: Color
      }
    })
    var OutCircle = new zrender.Circle({
      shape: {
        cx: 4,
        cy: 4,
        r: 4
      },
      style: {
        fill: '#FFFFFF',
        stroke: Color
      }
    })
    group.on('mouseover', function () {
      Text.show()

      Text.attr('position', [Obj.x, Obj.y - 32])
      Text.attr({
        style: {
          text: Obj.txt
        }
      })
    }).on('mouseout', function () {
      Text.hide()
    })

    group.add(OutCircle)
    group.add(Point)
    group.attr('position', [Obj.x - 3, Obj.y - 3])

    return group
  },
  // 绘制圆圈中有X的圆
  drawXCircle: function (Obj, Color) {
    var group = new zrender.Group()
    var h = new zrender.Text({
      style: {
        text: Obj.text,
        fontSize: 8,
        textFill: Color
      },
      position: [2, 1.5]
    })
    const OutCircle = new zrender.Circle({
      shape: {
        cx: 5,
        cy: 5,
        r: 5
      },
      style: {
        fill: '#FFFFFF',
        stroke: Color
      }
    })
    group.on('mouseover', function () {
      Text.show()
      Text.attr('position', [Obj.x, Obj.y - 32])
      Text.attr({
        style: {
          text: Obj.txt
        }
      })
    }).on('mouseout', function () {
      Text.hide()
    })

    group.add(OutCircle)
    group.add(h)
    group.attr('position', [Obj.x - 4, Obj.y - 4])

    return group
  }
}
